package com.example.mapdata.controller;

import com.example.mapdata.entity.User;
import com.example.mapdata.service.UserService;
import com.example.mapdata.utils.JwtUtils;
import com.example.mapdata.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2024-09-23 14:22:52
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录接口
     *
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/login")
    public ResultVo getToken(String username, String password) {
        boolean b = userService.getToken(username, password);
        log.info("查询到用户:{}", b);
        if (b) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("username", username);
            map.put("password", password);
            String jwt = JwtUtils.getJwt(map);
            return new ResultVo(jwt);
        } else {
            return new ResultVo("用户名或密码错误", false);
        }
    }

    /**
     * 注册一名新用户
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public ResultVo registerUser(@RequestBody User user) {
        boolean b = userService.registerUser(user);
        if (b) {
            return new ResultVo(200, "用户注册成功", true, user);
        } else {
            return new ResultVo("用户注册失败", false);
        }
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username
     * @return
     */
    @GetMapping("/find_user_info")
    public ResultVo findUserInfo(@RequestParam String username) {
        User user = userService.getUserInfoByUserName(username);
        if (user != null) {
            return new ResultVo(200, "查询成功", true, user);
        } else {
            return new ResultVo("查询失败", false);
        }
    }

    /**
     * 根据用户名修改用户信息
     *
     * @param user
     * @return
     */
    @PutMapping("/user_info")
    public ResultVo updateUserByUserName(@RequestParam String username, @RequestBody User user) {
        boolean b = userService.updateUserByUserName(username, user);
        if (b) {
            return new ResultVo(200, "用户信息修改成功", true, user);
        } else {
            return new ResultVo("用户信息修改失败", false);
        }
    }

    /**
     * 根据用户名删除用户
     *
     * @param username
     * @return
     */
    @DeleteMapping("/del")
    public ResultVo deleteUserByUserName(@RequestParam String username) {
        boolean b = userService.deleteUserByUserName(username);
        if (b) {
            return new ResultVo("用户删除成功", true);
        } else {
            return new ResultVo("用户删除失败", false);
        }
    }

    /**
     * 忘记密码 1. 根据手机号获取验证码
     *
     * @param phone
     * @param username
     * @return
     */
    @GetMapping("/code")
    public ResultVo forgetPassword(String phone, String username) {
        String code = userService.forgetPassword(phone, username);
        if (code != null) {
            return new ResultVo(200, "验证码发送成功", true, code);
        } else {
            return new ResultVo("验证码发送失败", false);
        }
    }

    /**
     * 忘记密码 2. 修改当前用户的密码
     *
     * @param map
     * @return
     */
    @PutMapping("/password")
    public ResultVo updateUserPassword(@RequestBody Map<String, String> map) {
        String password = map.get("password");
        String username = map.get("username");
        boolean b = userService.updateUserPassword(username, password);
        if (b) {
            return new ResultVo("密码修改成功", true);
        } else {
            return new ResultVo("密码修改失败", false);
        }
    }
}

